<?php

class Admin_TmcController extends ExtLb_Controller_Action{

	private $managerTbl;
	private $userTbl;
	private $lopTbl;
	private $monTbl;
	
	private $tmcSessionSet;
	private $tmcSessionGet;
	
    public function init()
    {
    	$this->loadCss('admin', 'tmc');
    	$this->loadLayout('admin', 'layout');
    	
        $this->managerTbl	= new Admin_Model_Manages();
        $this->userTbl		= new Admin_Model_User();
        $this->lopTbl		= new Admin_Model_Lops();
        $this->monTbl		= new Admin_Model_Monhoc();
        
        //
        // === Create session
        $this->tmcSessionSet =  new Zend_Session_Namespace('tmcSession');
        $this->refreshSession();
    }

    public function indexAction()
    {
        //
        // === List teacher
    	$this->view->teachs =  $this->userTbl
    										->listUser("quyen ='giaovien'")
    										->query()->fetchAll();;
    	//
    	// === List Mon học
    	$this->view->courses = $this->monTbl->fetchAllMonHoc()->toArray();

    	//
    	// === List lop
    	$this->view->lops = $this->lopTbl->getAllLop()->toArray();
    	
    	//
    	// === Bo xung lop cho giao vien
    	if(isset($_POST['cmdLAdd']) && !empty($_POST['course-check'])){
    		$arrLop = $_POST['course-check'];
    		for ($i =0; $i <count($arrLop); $i++){
//    			$this->managerTbl->addManage($this->tmcSessionGet['usernameTeach'], $arrLop[$i], $idMonHoc);
    		}
    	}
    	//
    	// === Manager Lop
    	if (isset($this->tmcSessionGet['usernameTeach'])){
    		$managerSelect = $this->managerTbl->selectManager("u.username = '".$this->tmcSessionGet['usernameTeach']."'");
    		
    		$paginatorManager = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($managerSelect));
    		$paginatorManager->setItemCountPerPage(20)
    						->setCurrentPageNumber($this->_getParam('page',1))
    						->setPageRange(3);
    		
    		$this->view->paginatorMangager = $paginatorManager;
    		$this->view->teachManager = $this->userTbl->fetchUser($this->tmcSessionGet['usernameTeach'])->toArray();
    	}
    }
    
    public function addAction(){
		if(isset($_POST['cmdAdd'])){
			$teach 	= $this->_getParam('cbbTeach');
			$lop	= $this->_getParam('cbbClass') ;
			$course	= $this->_getParam('cbbCourse');
			
			try {
				$this->managerTbl->addManage($teach, $lop, $course);
				$this->jDialogMessage('Thông báo','Thành công!!!');
				$this->_forward('index','tmc','admin');
				
			} catch (Exception $e) {
				$this->jDialogMessage("Cảnh báo", "Đã có lỗi xảy trong trong khi xử lý. Có thể là môn học và lớp học này đã được thêm vào trong CSDL. Vui lòng kiểm tra lại!!!");
				$this->_forward('index','tmc','admin');
			}
			
		}else{
			$this->_redirect('/admin/tmc/');
		}    	
    }

    public function viewmanagerAction(){
//    	$this->view->teachs = $this->userTbl
//    								->listUser("quyen ='giaovien'")
//    								->query()->fetchAll();;
    	
		if(isset($_POST['cmdView'])){
			//
			// === Luu session
			$this->tmcSessionSet->usernameTeach = $this->_getParam('cbbTeachView');
			$this->refreshSession();
			
			$this->view->managers = $this->managerTbl
										->selectManager("u.username ='".$this->_getParam('cbbTeachView')."'")
										->query()->fetchAll();
		}
    	
    	$this->_forward('index','tmc','admin',array('a'=>12));
    }
    
    public function delAction(){
    	if(!empty($_GET['course']) && isset($_GET['lop']) && !empty($_GET['lop'])){
    		$this->managerTbl->deleteManage(
    								"idMonHoc =".$_GET['course']." 
    								and username = '".$this->tmcSessionGet['usernameTeach']."'
    								and idLop = '".$_GET['lop']."'"
    					);
    	}
    	$this->_forward('index','tmc','admin');
    }
	private function refreshSession(){
    	$this->tmcSessionGet = Zend_Session::namespaceGet('tmcSession');
    }

}



